package cn.lili.modules.grab.xdxt.service.impl;

import cn.lili.modules.grab.xdxt.config.XdxtConfig;
import cn.lili.modules.grab.xdxt.reps.goods.GoodsCateReps;
import cn.lili.modules.grab.xdxt.reps.goods.ListGoodsReps;
import cn.lili.modules.grab.xdxt.res.goods.ListGoodsRes;
import cn.lili.modules.grab.xdxt.service.XdxtGoodsService;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.schl.api.utils.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class XdxtGoodsServiceImpl implements XdxtGoodsService {

    @Resource
    private XdxtConfig xdxtConfig;

    @Override
    public GoodsCateReps goodsCate() throws JsonProcessingException {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", xdxtConfig.getAppid());
        paramMap.put("secret", xdxtConfig.getSecret());
        JSONObject result = RequestUtil.queryGoodsCategory(paramMap, xdxtConfig.getUrl());
        return GoodsCateReps.builderGoodsCateReps(result);
    }

    @Override
    public ListGoodsReps goodsList(ListGoodsRes listGoodsRes) throws JsonProcessingException {
        log.info("盛创汇联--商品列表入参----{}", JSONObject.toJSONString(listGoodsRes));

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", xdxtConfig.getAppid());
        paramMap.put("secret", xdxtConfig.getSecret());
        paramMap.put("current", listGoodsRes.getCurrent());
        paramMap.put("size", listGoodsRes.getSize());

        Map<String, Object> query = new HashMap<>();
        query.put("queryType", listGoodsRes.getQuery().getQueryType());
        query.put("goodsIds", listGoodsRes.getQuery().getGoodsIds());
        paramMap.put("query", query);
        JSONObject result = RequestUtil.queryGoods(paramMap, xdxtConfig.getUrl());
        log.info("盛创汇联--商品列表出参----{}", JSONObject.toJSONString(result));
        return ListGoodsReps.builderListGoodsReps(result);
    }
}
